package com.sdry.service.impl.llm;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.llm.EmployeeMapper;
import com.sdry.mapper.llm.ProductionReportMapper;
import com.sdry.mapper.llm.TeamMapper;
import com.sdry.mapper.llm.TeamRecordMapper;
import com.sdry.mapper.llm.UserMapper;
import com.sdry.model.llm.Employee;
import com.sdry.model.llm.Team;
import com.sdry.model.llm.TeamRecord;
import com.sdry.model.llm.User;
import com.sdry.service.llm.EmployeeService;
import com.sdry.service.llm.TeamService;


/** 
* @author lian 
* @version 1.0
* @time 2018年10月17日 上午12:56:50
*/
@Service
public class TeamServiceImp implements TeamService {

	@Resource
	private TeamMapper teamMapper;  //注入班组mapper
	@Resource
	private EmployeeMapper employeeMapper; //注入员工管理mapper
	@Resource
	private UserMapper userMapper; //注入员工mapper
	@Resource
	private TeamRecordMapper teamRecordMapper; //注入班组记录mapper
	@Resource
	private EmployeeService empService; //注入员工管理service
	@Resource
	private ProductionReportMapper productionReportMapper;
	
	
	/**
	 * 查询班组数据总行数
	 * @param team 班组
	 * @return
	 */
	@Override
	public int countTeam(Team team) {
		return teamMapper.countTeam(team);
	}

	/**
	 * 分页查询班组
	 * @param map 
	 * @return
	 */
	@Override
	public List<Team> listPageTeam(Map<String, Object> map) {
		List<Team> list = teamMapper.listPageTeam(map);
		for(Team team : list){
			if(null != team.getNum() && !"".equals(team.getNum())){
				User user = userMapper.findUserByUid(team.getNum());
				team.setForeman(user.getUserName());
			}
		}
		
		return list;
	}

	/**
	 * 新增班组
	 * @param team 班组
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public int insertTeam(Team team){
		int isSuccess = -2;
		Employee emp = new Employee();
		try {
			List<Team> list = teamMapper.getTeamByName(team.getTname());
			if(list.size() == 0){
				//1、获取班组长的用户信息
				if(null != team.getNum() && !"".equals(team.getNum())){
					emp.setUid(team.getNum());
					Team newTeam = productionReportMapper.getTeamByTeamName(team.getTname());
					if(null == newTeam){
						//1、保存班组表
						int teamId = teamMapper.insertTeam(team); 
						if(teamId > 0){
							emp.setTid(teamId + 0L);
							//1、把员工和班组的关系保存到员工表中
							int i  = empService.insertEmp(emp);
							if(i <= 0){
								//如果员工表保存失败，把新增的班组表数据删除
								teamMapper.deleteTeam(teamId+0L);
							}else{
								isSuccess = 2;
							}
						}
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

	/**
	 * 修改班组
	 * @param team 班组
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public int updateTeam(Team team) {
		int isSuccess = -3;
		Employee emp = new Employee();
		try {
			//1、获取班组长的用户信息
			if(null != team.getNum() && !"".equals(team.getNum())){
				emp.setUid(team.getNum());
				emp.setTid(team.getTid());
				
				Team tempTeam = teamMapper.getTeamByTid(team.getTid());
				//2.1 如果要修改的班组名称还是原来的班组名称
				if(team.getTname().equals(tempTeam.getTname())){
					//2.1.2如果班组长没修改
					if(team.getNum().equals(tempTeam.getNum())){
						int i3 = teamMapper.updateTeam(team);
						if(i3 > 0){
							isSuccess = 3;
						}
					}else{
						//1、把员工和班组的关系保存到员工表中
						int i = empService.insertEmp(emp);
						if(i > 0){
							//2、修改班组表
							int i2 = teamMapper.updateTeam(team);
							if(i2 <= 0){
								//如果班组表修改失败，把新增的员工表数据删除
								employeeMapper.deleteEmp(i + 0L);
							}else{
								employeeMapper.deleteEmpByUidAndTid(tempTeam);
								isSuccess = 3;
							}
						}
					}
				}else{
					//2.2 如果修改班组名称，判定是否重复
					Team newTeam = productionReportMapper.getTeamByTeamName(team.getTname());
					if(null == newTeam){
						//2.2.2如果班组长没修改
						if(team.getNum().equals(tempTeam.getNum())){
							int i3 = teamMapper.updateTeam(team);
							if(i3 > 0){
								isSuccess = 3;
							}
						}else{
							//1、把员工和班组的关系保存到员工表中
							int i = empService.insertEmp(emp);
							if(i > 0){
								//2、修改班组表
								int i2 = teamMapper.updateTeam(team);
								if(i2 <= 0){
									//如果班组表修改失败，把新增的员工表数据删除
									employeeMapper.deleteEmp(i + 0L);
								}else{
									employeeMapper.deleteEmpByUidAndTid(tempTeam);
									isSuccess = 3;
								}
							}
						}
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

	/**
	 * 删除班组
	 * @param tid 班组ID
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public int deleteTeam(String str) {
		int isSuccess = 0;
		String[] split = str.split(",");
		try {
			for (String tid : split) {
				isSuccess = teamMapper.deleteTeam(Long.parseLong(tid));
				if(isSuccess > 0){
					teamMapper.deleteTeamInEmp(Long.parseLong(tid));
				}
			}
		}catch (
		Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

	/**
	 * 查询所有班组
	 * @return
	 */
	@Override
	public List<Team> listAllTeam() {
		return teamMapper.listAllTeam();
	}

	/**
	 * 查询班组的员工总数
	 * @param tid
	 * @return
	 */
	@Override
	public int countUserByTeam(Long tid) {
		return teamMapper.countUserByTeam(tid);
	}

	/**
	 * 查询班组的员工详细数据
	 * @param map
	 * @return
	 */
	@Override
	public List<User> listPageUserByTeam(Map<String, Object> map) {
		return teamMapper.listPageUserByTeam(map);
	}

	/**
	 * 删除班组中的某个员工
	 * @param emp
	 * @return
	 */
	@Override
	public int deleteUserInEmp(Employee emp) {
		return teamMapper.deleteUserInEmp(emp);
	}

	/**
	 * 根据员工ID获取班组信息
	 * @param uid 员工ID
	 * @return
	 */
	@Override
	public Team getTeamByUid(Long uid) {
		return teamMapper.getTeamByUid(uid);
	}

	/**
	 * 校验是否可以删除班组
	 * @param str 班组ID组成的字符串
	 * @return
	 * @see com.sdry.service.llm.TeamService#deleteTeamOfVerify(java.lang.String)
	 */
	@Override
	public int deleteTeamOfVerify(String str) {
		int isSuccess = -1;
		String[] split = str.split(",");
		for(int i = 0; i < split.length; i ++){
			Long id = Long.parseLong(split[i]);
			List<Employee> empList = employeeMapper.findEmployeeByTid(id);
			List<TeamRecord> trList = teamRecordMapper.getTeamRecordByTeamId(id);
			if(empList.size() > 0 || trList.size() > 0){
				isSuccess = 1;
				break;
			}
		}
		return isSuccess;
	}

	/**     
	 * 查询员工是否是班组长  
	 * @param team
	 * @return    
	 */
	@Override
	public List<Team> findTeamByUidInTeam(Team team) {
		return teamMapper.getTeamsByUid(team.getNum());
	}

}
